/* 
* @Author: Marte
* @Date:   2018-01-04 14:49:09
* @Last Modified by:   Marte
* @Last Modified time: 2018-01-05 19:07:12
*/
var formatDate = function(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();

    if (mymonth < 10) {
        mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday;
    }

    return (myyear + "-" + mymonth + "-" + myweekday);
};

function todyatime(){
var d = new Date();
var my = d.getFullYear();
var mm = d.getMonth() + 1;
var md = d.getDate();
if (mm < 10) {
        mm = "0" + mm;
 }
if (md < 10) {
        md = "0" + md;
 }
var weekday=new Array(7);
weekday[0]="星期天";
weekday[1]="星期一";
weekday[2]="星期二";
weekday[3]="星期三";
weekday[4]="星期四";
weekday[5]="星期五";
weekday[6]="星期六";
return  (my+'-'+mm+'-'+md+'日'+'  '+weekday[d.getDate()]);

}


$("#today").text(todyatime());


$(document).ready(function() {
comp_rate('pmselect1');
comp_rate('amselect1');

    $.ajax({
        url: '/mainweekreport/getstaffname.do',
        type: 'GET',
        async: true,
        dataType: 'json',
        success: function(data) {

            $.each(data,
            function(key, item) {

                $("#staffName").append("<option value=" + key + ">" + item + "</option>");

            });

        },
        error: function() {

            alert("找名字的时候出错了");
        },

    });

    for (var i = 0; i < 5; i++) {
        $("#thisweektable").append("<tr>" + "<td>" + (i + 1) + "</td>" + "<td><input class='form-control' id='thisweek_content_0'" + (i + 1) + " name='thisweek_content_0'" + (i + 1) + " type='text'/></td>" + "<td><input  class='form-control'  id='thisweek_risk_0'" + (i + 1) + " name='thisweek_risk_0'" + (i + 1) + " type='text' value='暂无'/></td>" + "<td><input  class='form-control' id='thisweek_advice_0'" + (i + 1) + " name='thisweek_advice_0'" + (i + 1) + " type='text' value='暂无'/></td>" + +"</tr>");
    }

    for (var i = 0; i < 5; i++) {
        $("#nextweektable").append("<tr>" + "<td>" + (i + 1) + "</td>" + "<td><input class='form-control' id='nextweek_content_0'" + (i + 1) + " name='nextweek_content_0'" + (i + 1) + " type='text'/></td>" + "<td><input  class='form-control' id='nextweek_risk_0'" + (i + 1) + " name='nextweek_risk_0'" + (i + 1) + " type='text' value='暂无'/></td>" + "<td><input  class='form-control' id='nextweek_advice_0'" + (i + 1) + " name='nextweek_advice_0'" + (i + 1) + " type='text' value='暂无'/></td>" + +"</tr>");
    }

    var getStrByUsrvalue = function(pattern) {

        var contentlist = $("input[name^='" + pattern + "']");
        var str = '';
        $.each(contentlist,
        function(key, item) {
            str += $.base64.encode(encodeURIComponent(item.value)) + ",";
        });
        return str;
    };
        var getStrByUsrvalue1 = function(pattern) {

        var options = $("select[name^='" + pattern + "']");
        var str = '';
        
        $.each(options,
        function(key, item) {
            /*console.log(item.value);*/
            str += $.base64.encode(encodeURIComponent(item.value)) + ",";
        });
        return str;
    };

    $("#downloadExcelReport").bind("click",
    function() {

        $("#reportStartdate").val(startdate);
        $("#reportForm").submit();

    });

    $("#submitlost").click(function() {
        $.ajax({
            url: '/mainweekreport/submitlost.do',
            type: 'post',
            async: true,
            dataType: 'json',
            contentType: "application/json",

            success: function(data) {

                if (data) {
                    if (data.length == 0) {
                        alert("哈哈哈已经全都提交了");
                    } else {

                        alert("还有" + data.length + "人专心工作  没写周报  其中包括:" + data + "  你们这样让我很为难啊!!");
                    }
                }

            },
            error: function() {

                alert("搜索谁没提交时发生不明异常");
            },

        })
    });

$("#submitreport").bind('click', function() {
    /* var name = $("#staffName option:selected").val();
     var realname = $("#staffName option:selected").text();
        if (name == 0) {
            alert("咱能先选个名字么？");
            return;
        }*/
            


 var thisweekcontentlist = $("input[name^='am']");
            var str = '';
            var flag = false;
            $.each(thisweekcontentlist, function (key, item) {
                if (!$.trim(item.value) == '') {
                    flag = true;
                }
            });

            if (!flag) {
                alert("浪费时间叫虚度，剥用时间叫生活。");
                return;
            }

            var pam={};
             pam.am = getStrByUsrvalue('am');
             pam.amselect = getStrByUsrvalue1('amselect');
             pam.ateam = getStrByUsrvalue('ateam');
             pam.pm = getStrByUsrvalue('pm');
             pam.pmselect = getStrByUsrvalue1('pmselect');
             pam. pteam = getStrByUsrvalue('pteam');
             pam.name=$("#staffName option:selected").val();
             // console.log(pam);
             $.ajax({
            url: '/mainweekreport/submitweekreport.do',
            type: 'post',
            async: true,
            dataType: 'json',
            contentType: "application/json",
         /*   data: {
                "name": name,
                "today": today,
                "am": am,
                "amselect": amselect,
                "ateam": ateam,
                "pm": pm,
                "pmselect": pm,
                "pteam": pteam
            },*/
            data:pam,
            success: function(data) {

                if (data.result == "repeat") {
                    alert("hi," + realname + "小朋友，暂提交一次就好。");
                } else {
                    alert("多谢" + realname + "小朋友，恭喜你啦，可以放假啦！");
                }

            },
            error: function() {

                alert("提交周报时出错了");
            },

        });


       
});
});
function alert(msg, callback) {
    var title = '信息';
    var dialogHTML = '<div id="selfAlert" class="modal fade">';
    dialogHTML += '<div class="modal-dialog" style="width: 300px">';
    dialogHTML += '<div class="modal-content">';
    dialogHTML += '<div class="modal-header">';
    dialogHTML += '<button type="button" class="close" data-dismiss="modal" aria-label="Close">';
    dialogHTML += '<span aria-hidden="true">&times;</span>';
    dialogHTML += '</button>';
    dialogHTML += '<h4 class="modal-title">' + title + '</h4>';
    dialogHTML += '</div>';
    dialogHTML += '<div class="modal-body">';
    dialogHTML += msg;
    dialogHTML += '</div>';
    dialogHTML += '<div class="modal-footer">';
    dialogHTML += '<button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>';
    dialogHTML += '</div>';
    dialogHTML += '</div>';
    dialogHTML += '</div>';
    dialogHTML += '</div>';

    if ($('#selfAlert').length <= 0) {
        $('body').append(dialogHTML);
    }

    $('#selfAlert').on('hidden.bs.modal',
    function() {
        $('#selfAlert').remove();
        if (typeof callback == 'function') {
            callback();
        }
    }).modal({
        backdrop: 'static'
    });
}



function comp_rate(val){
    for (var i = 1; i < 11; i++) {
        $("#"+val).append("<option value='" + i + "'>" + 10* i+"%</option>");

    }
};


var j=1;
var m=1;
$("#addbtn1").click(function() {
   j+=1;
    var add ="<li id='item_list_"+j+"'name='list_data'><div class='col-md-4 column'><input type='text' name='am' placeholder='请输入工作安排' class='form-control input_style'/></div><div class='col-md-4 column'><select name='amselect' class='selectpicker show-tick form-control margin-top1' data-live-search='false' id='amselect_"+j+"'><option value=''>请选择</option><option value='1'>10%</option><option value='2'>20%</option><option value='3'>30%</option><option value='4'>40%</option><option value='5'>50%</option><option value='6'>60%</option><option value='7'>70%</option><option value='8'>80%</option><option value='9'>90%</option><option value='0'>100%</option></select></div><div class='col-md-4 column'><input type='text' name='ateam' placeholder='请输入所属项目' class='input_style form-control'/></div></li>";
    $(add).insertBefore($("#addmenu1"));
});
$("#addbtn2").click(function() {
   m+=1;
   var add1 ="<li id='item_list_"+m+"'><div class='col-md-4 column'><input type='text' name='pm' placeholder='请输入工作安排' class='form-control input_style'/></div><div class='col-md-4 column'><select name='pmselect' class='selectpicker show-tick form-control margin-top1' data-live-search='false' id='pmselect_"+m+"><option value=''>请选择</option><option value=''>请选择</option><option value='1'>10%</option><option value='2'>20%</option><option value='3'>30%</option><option value='4'>40%</option><option value='5'>50%</option><option value='6'>60%</option><option value='7'>70%</option><option value='8'>80%</option><option value='9'>90%</option><option value='0'>100%</option></select></div><div class='col-md-4 column'><input type='text' name='pteam' placeholder='请输入所属项目' class='input_style form-control'/></div></li>";
    $(add1).insertBefore($("#addmenu2"));
});




